const readlineSync = require('readline-sync');
const path = require('path');
const fs = require('fs');
const copy = require('recursive-copy');
const template = require('art-template');

function makeDirs(fullPath) {
    if (!fs.existsSync(fullPath)) {
        makeDirs(path.dirname(fullPath));
        fs.mkdirSync(fullPath);
    }
}

function checkProjectPath(projectPath) {
    projectPath = projectPath || '';
    projectPath = projectPath.trimEnd().trimStart();
    return !!projectPath;
}

function checkProjectName(name) {
    let re = /^[A-Za-z0-9\-]+$/g;
    return re.test(name);
}

// 项目所在文件夹， 不包含项目本身文件夹
let projectPath = readlineSync.question('Project path:');
while(!checkProjectPath(projectPath)) {
    projectPath = readlineSync.question('Please input project path:');
}

// 项目名称
let projectName = readlineSync.question('Project Name:');
while(!checkProjectName(projectName)) {
    console.log('Porject name just support "a-z|A-Z|-", please input again');
    projectName = readlineSync.question('Please input project Name:');
}

// 项目打包文件所在文件夹
let outputFolder = readlineSync.question('Output Folder (Default folder is same with project name ):');
outputFolder = outputFolder.trimStart().trimEnd() || projectName;

// 项目初始版本号
let version = readlineSync.question('Version (default 0.0.1):');
version = version.trimStart().trimEnd() || '0.0.1';

// 项目描述 - 将出现在html title中
let description = readlineSync.question('Description:');
description = description.trimStart().trimEnd() || '';

// 项目作者
let author = readlineSync.question('Auther:');
author = author.trimStart().trimEnd() || '';

// 本地开发端口
let devPort = readlineSync.question('Develop port (Default 5001):');
devPort = devPort.trimStart().trimEnd() || 5001;

const templateParams = {
    projectName,
    version,
    description,
    author,
    outputFolder,
    devPort
};
const targetFolder = path.join(projectPath, projectName);
makeDirs(targetFolder);

// 从template文件夹下复制模板文件到新建的项目目录下
const copyOptions = {
    dot: true
};
copy('./template', targetFolder, copyOptions)
    .then(res => {
        console.info('Copied ' + res.length + ' files');
        rednerTemplate();
        renderGitIgnore();
    })
    .catch(err => {
        console.error('Copied failed' + err);
    });

// 根据用户输入参数 替换模板文件中的内容
function rednerTemplate () {
    const TemplateFiles = [
        'package.json',
        'src/index.html',
        'src/index-dev.html',
        'src/main.js',
        'config/dev.js',
        'config/prod.js'
    ];
    TemplateFiles.forEach(file => {
        const content = template(path.resolve('./template', file), templateParams);
        fs.writeFileSync(path.join(targetFolder, file), content);
    });
}

// 写入 .gitignore 文件
function renderGitIgnore() {
    const ignoreContent = `.DS_Store
node_modules/
/lib/
/dist/

package-lock.json
pnpm-lock.yaml
expressServer.js

# local env files
.env.local
.env.*.local

# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
yarn.lock

# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?`
    let ignoreFile = path.join(targetFolder, '.gitignore');
    fs.writeFileSync(ignoreFile, ignoreContent);
}